from django import template
from django.conf import settings
from django.templatetags.static import static
import os

register = template.Library()


@register.simple_tag(takes_context=True)
def get_current_template_css(context, *args, **kwargs):
    template_suffix = kwargs.get("template_suffix", ".html")
    group = kwargs.get("group", "default")
    try:
        # 获取当前模板的名称
        current_template_name = context.template_name
        print(current_template_name)
        # 如果模板名称是列表或元组，取第一个元素作为文件名
        if isinstance(current_template_name, (list, tuple)):
            current_template_name = template[0]
        current_template_name = current_template_name.replace(template_suffix, '.css')
        current_template_name = os.path.normpath(current_template_name)
        current_template_name = current_template_name.split(os.sep)
        current_template_name = current_template_name[:-1] + [group, 'css'] + [current_template_name[-1]]
        return static(os.path.join(*current_template_name))
    except AttributeError:
        return "未知模板"
@register.simple_tag(takes_context=True)
def get_current_template_js(context, *args, **kwargs):
    template_suffix = kwargs.get("template_suffix", ".html")
    group = kwargs.get("group", "default")
    try:
        # 获取当前模板的名称
        current_template_name = context.template_name
        print(current_template_name)
        # 如果模板名称是列表或元组，取第一个元素作为文件名
        if isinstance(current_template_name, (list, tuple)):
            current_template_name = template[0]
        current_template_name = current_template_name.replace(template_suffix, '.js')
        current_template_name = os.path.normpath(current_template_name)
        current_template_name = current_template_name.split(os.sep)
        current_template_name = current_template_name[:-1] + [group, 'js'] + [current_template_name[-1]]
        return static(os.path.join(*current_template_name))
    except AttributeError:
        return "未知模板"